/**
 * uploadFile 上传w文件组件 配合form组件使用
 * @Author: linzy
 * @Date: 2022-05-31
 * @function Upload
 * @param {number} minCount - 最少上传数量
 * @param {number} maxCount=1 - 最多上传数量
 * @param {number} maxSize - 大小 单位为兆
 * @param {boolean} accept=* - 上传的文件类型
 * @param {boolean} listType=picture - 上传文件展示模式
 * @param {*} ... - antd Upload组件属性
 * @example
 *  <UploadFile />
 */
import React from "react";
import { DJUpload } from "@/components";
import { Icon, message } from "antd";
import _ from "lodash";
import { setOutAttrs, setDefaultAttrs } from "@/hooks/tools";
import { useConstants } from "@/hooks";

export default function (props) {
  const { FILE_SERVER } = useConstants();
  let {
    minCount,
    maxCount = 1,
    maxSize, // 兆为单位
    onPreview = (file) => {
      const url = file.url ? file.url : FILE_SERVER + file.response?.id;
      window.open(url, "_blank");
    },
    ...restProps
  } = props;
  minCount && maxCount < minCount && (maxCount = minCount);
  setOutAttrs(restProps, { onPreview });
  setDefaultAttrs(restProps, {
    accept: "*",
    listType: "picture-card",
  });
  const uploadButton = (
    <div>
      <Icon type="plus" />
      <div className="ant-upload-text">上传文件</div>
    </div>
  );
  const preCount = props.fileList?.length || 0;
  restProps.children = restProps.children
    ? restProps.children
    : preCount < maxCount
      ? uploadButton
      : null;
  /**
   * 获取警告前缀
   *
   * @param {any[]} fileList
   * @param {number[]} indexList
   * @returns {string}
   */
  function getPreMsg(fileList, indexList) {
    return fileList.length > 1 ? `第${indexList.join("、")}个` : "";
  }
  props.beforeUpload || (props.beforeUpload = function (file, fileList) {
    return new Promise((resolve, reject) => {
      // 个数判断
      if (maxCount) {
        if (fileList.length + preCount > maxCount) {
          message.destroy();
          message.error(`文件数量最多不能超过${maxCount}个，请重新上传`);
          reject([]);
          return;
        }
      }
      if (restProps.accept) {
        let rejectList = [];
        let indexList = [];
        _.filter(fileList, (item, index) => {
          if (item.type.indexOf(restProps.accept.replace(/\*/, "")) === -1) {
            rejectList.push(item);
            indexList.push(index + 1);
          }
        });
        if (rejectList.length) {
          message.error(
            getPreMsg(fileList, indexList) +
            `文件类型不符合，请上传${restProps.accept}类型的文件`
          );
          reject([]);
        }
      }
      if (maxSize) {
        let rejectList = [];
        let indexList = [];
        _.each(fileList, (item, index) => {
          if (item.size / (1024 * 1024) > maxSize) {
            rejectList.push(item);
            indexList.push(index + 1);
          }
        });
        if (rejectList.length) {
          message.destroy();
          message.error(
            getPreMsg(fileList, indexList) +
            `文件大小超过${maxSize}M，请重新上传`
          );
          reject([]);
          return;
        }
      }
      resolve();
    });
  });
  return (
    <>
      <DJUpload {...restProps} />
    </>
  );
}
